﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;

namespace CP.Repositories.GPM
{
    public class DepartmentRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Department> GetAllDepts()
        {
            return db.ExecuteObject<Department>("select * from department order by dept_id");
        }

        public Department GetDeptByID(int deptid)
        {
            IList<Department> depts = db.ExecuteObject<Department>(string.Format(@"select * from department
                                        where dept_id = {0} ", deptid));
            if (depts.Count > 0)
                return depts[0];
            return null;
        }

        public bool DeleteDept(IList<int> ids)
        {
            try
            {
                db.BeginTransaction();
                foreach (int id in ids)
                {
                    db.ExecuteNonQuery(string.Format(@"delete department 
                                        where dept_id = {0} ", id));
                }
                db.Commit();
                return true;
            }

            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool AddDept(Department dept)
        {
            db.ExecuteNonQuery(string.Format(@"insert into department(dept_id,dept_name) 
                                        values('{0}','{1}') ", dept.dept_id, dept.dept_name));
            return true;
        }

        public bool UpdateDept(Department dept)
        {
            db.ExecuteNonQuery(string.Format(@"update department set dept_name = '{1}'  
                                     where dept_id = '{0}' ", dept.dept_id, dept.dept_name));
            return true;
        }
    }
}
